package ks.app.smartDic;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import java.util.List;

public class httpClient {

	DefaultHttpClient client;
	URI uri;
	HttpGet method;
	HttpResponse res;
	private InputStream data;
	InputStreamReader reader;
	String getEncoding;

	httpClient() {
		client = new DefaultHttpClient();
	}

	public InputStream getUrlData(String url) {

		try {
			uri = new URI(url);
			method = new HttpGet(uri);
			res = client.execute(method);
			data = res.getEntity().getContent();

		} catch (ClientProtocolException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		} catch (URISyntaxException e) {

			e.printStackTrace();
		}

		return data;
	}
	
	public InputStream post(String url) {

		try {
			HttpPost methodPost = new HttpPost(url);

			res = client.execute(methodPost);
			data = res.getEntity().getContent();

		} catch (ClientProtocolException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}

		return data;
	}
	
	public InputStream postEUCKR(String url, List<NameValuePair> nvps) {

		try {
			HttpPost methodPost = new HttpPost(url);

			methodPost.setEntity(new UrlEncodedFormEntity(nvps, "EUC-KR"));

			res = client.execute(methodPost);
			data = res.getEntity().getContent();

		} catch (ClientProtocolException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}

		return data;
	}
	

	public InputStream post(String url, List<NameValuePair> nvps) {

		try {
			HttpPost methodPost = new HttpPost(url);

			methodPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

			res = client.execute(methodPost);
			data = res.getEntity().getContent();

		} catch (ClientProtocolException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}

		return data;
	}

	public String generateString(InputStream stream) {

		Charset charset = java.nio.charset.Charset.forName("UTF-8");
		reader = new InputStreamReader(stream, charset);
		BufferedReader buffer = new BufferedReader(reader);
		StringBuilder sb = new StringBuilder();

		try {
			String cur;
			while ((cur = buffer.readLine()) != null) {
				sb.append(cur + "\n");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			stream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	public String generateString() {

		Charset charset = java.nio.charset.Charset.forName("UTF-8");
		reader = new InputStreamReader(data, charset);
		BufferedReader buffer = new BufferedReader(reader);
		StringBuilder sb = new StringBuilder();

		try {
			String cur;
			while ((cur = buffer.readLine()) != null) {
				sb.append(cur + "\n");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			data.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	public String postAndGetStr(String url)
	{
		try {
			HttpPost methodPost = new HttpPost(url);

			res = client.execute(methodPost);
			data = res.getEntity().getContent();

		} catch (ClientProtocolException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
		
		Charset charset = java.nio.charset.Charset.forName("UTF-8");
		reader = new InputStreamReader(data, charset);
		BufferedReader buffer = new BufferedReader(reader);
		StringBuilder sb = new StringBuilder();

		try {
			String cur;
			while ((cur = buffer.readLine()) != null) {
				sb.append(cur);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			data.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
		
	}
		
}
